package com.example.question.array;

import java.util.ArrayList;
import java.util.List;

/**
 * @Date 2023-08-03
 * @Author Jonathan
 */
public class Code01_442 {
    public static void main(String[] args) {

    }

    public List<Integer> findDuplicates1(int[] nums) {
        List<Integer> res = new ArrayList<>();
        int[] countMap = new int[nums.length];
        for (int num : nums) {
            countMap[num - 1]++;
            if (countMap[num - 1] == 2) {
                res.add(num);
            }
        }
        return res;
    }

    List<Integer> findDuplicates(int[] nums) {
        List<Integer> res = new ArrayList<>();
        for (int i = 0; i < nums.length; i++) {
            int index = Math.abs(nums[i] - 1);
            if (nums[index] < 0) {
                res.add(Math.abs(nums[i]));
            } else {
                nums[index] = -nums[index];
            }
        }
        return res;

    }

}
